<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/task.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
    String groupId = input.getDesc("group_id", "");
    List<GroupPatient> groupPatients = QBuilder.db("basedb").select().from("GroupPatient").where(new Where("status = ", STATUS_ENABLE).and("hospital =", hospital).and("groupId=", groupId)).query().getResultObj();
    if (isEmpty(groupPatients)) {
        msg = new Message(30041, "该分组下还未添加用户");
    } else {
        String uids = "-1";
        List<Map> list = new ArrayList<Map>();
        for (int i = 0; i < groupPatients.size(); i++) {
            Map map = new HashMap();
            list.add(map);
            GroupPatient groupPatient = groupPatients.get(i);
            map.put("group_patient_id", groupPatient.getId());
            map.put("patient_uid", groupPatient.getPatientUid());
            uids += "," + Tools.getDesc(groupPatient.getPatientUid(), "-1");
        }
        List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("status= ", STATUS_ENABLE).and("hospital=", hospital).and("SELF=", SELF).and("uid in ("+ uids +")")).query().getResultObj();
        List<String> pUids = QBuilder.db("basedb").select("uid").from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", 8).and("o.operate=", 2).and("o.status=", STATUS_ENABLE).and("o.oid=", token.getUid())).query().getResultObj();
        List<User> users = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status= ", STATUS_ENABLE).and("u.hospital=", hospital).and("u.id in ("+ uids +")")).query().getResultObj();
        Iterator<User> it = users.iterator();
        while(it.hasNext()) {
            User u = it.next();
            String uid = u.getId() + "";
            if (!pUids.contains(uid))
                it.remove();
        }
        Iterator<Map> iter = list.iterator();
        while(iter.hasNext()) {
            Map u = iter.next();
            String uid = u.get("patient_uid") + "";
            if (!pUids.contains(uid))
                iter.remove();
        }
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            String patientUid = map.get("patient_uid") + "";
            for (int j = 0; j < userProfiles.size(); j++) {
                UserProfile userProfile = userProfiles.get(j);
                String uid = userProfile.getUid() + "";
                if (patientUid.equals(uid)) {
                    Long birth = Tools.getLong(userProfile.getBirthday() + "", -1L);
                    map.put("birthday", birth < 0 ? "" : DateUtil.formatDate(new Date(birth)));
                    map.put("gender", Tools.getDesc(userProfile.getGender() + "", ""));
                    map.put("self", Tools.getDesc(userProfile.getSelf() + "", "0"));
                    map.put("age", Tools.getDesc(userProfile.getAge() + "", ""));
                    map.put("phone", Tools.getDesc(userProfile.getPhone() + "", ""));
                    map.put("card_type", Tools.getDesc(userProfile.getCardType() + "", ""));
                    map.put("medical_type", Tools.getDesc(userProfile.getMedicalType() + "", ""));
                    map.put("name", Tools.getDesc(userProfile.getName() + "", ""));
                    map.put("json", userProfile.getJson());
                    userProfiles.remove(userProfile);
                    break;
                }
            }
            for (int j = 0; j < users.size(); j++) {
                User user = users.get(j);
                String uid = user.getId() + "";
                if (patientUid.equals(uid)) {
                    map.put("avatar", getDefaultAvatar(user));
                    map.put("name", !isEmpty(user.getName())?user.getName():!isEmpty(user.getNickName())?user.getNickName():user.getUserName());
                    users.remove(user);
                    break;
                }
            }
        }
        msg.put("data", list);
    }
    Tools.encryptPrint(out, msg);
%>
